/**
 * 
 */
package com.da.system;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.eap.sa.Privilege;
import com.espirit.eap.sa.ScopeType;
import com.googlecode.cswish.struts.spring.GenericService;

/**
 * 初始化功能权限数据
 * @author Jebel Qin
 * @date 2015年6月18日
 * @since v1.0
 */

@Service
public class AuthDataService
{	
	private static String[] privArray = {
		"ra.landlord.landlord.getLandlordOwnerUser",  //RA module begin 
		"ra.landlord.building.searchBuilding",
		"ra.landlord.building.addBuilding",
		"ra.landlord.building.updateBuilding",
		"ra.landlord.building.deleteBuilding",
		"ra.landlord.roomItemCfg.searchRoomItemCfg",
		"ra.landlord.room.addRoom",
		"ra.landlord.room.updateRoom",
		"ra.landlord.room.deleteRoom",
		"ra.landlord.shareUnit.searchShareUnit",
		"ra.landlord.shareUnit.addShareUnit",
		"ra.landlord. shareUnit.updateShareUnit",
		"ra.landlord.shareUnit.deleteShareUnit",
		"ra.landlord.shareUnit.searchWithMeter",
		"ra.landlord.feeRuleCfgDesc.searchShareMeter",
		"ra.landlord.feeRuleCfgDesc.searchFixed",
		"ra.landlord.feeRuleCfgDesc.searchMeter",
		"ra.landlord.feeRuleCfg.searchMeterFeeRule",
		"ra.landlord.feeRuleCfgDesc.addFeeRuleCfgDesc",
		"ra.landlord.feeRuleCfgDesc.updateFeeRuleCfgDesc",
		"ra.landlord.feeRuleCfgDesc.deleteFeeRuleCfgDesc",
		"ra.landlord.feeRuleCfg.addFeeRule",
		"ra.landlord.feeRuleCfg.deleteFeeRuleCfg",
		"ra.landlord.feeRuleCfg.addFeeRuleCfg",
		"ra.landlord.meter.searchMeterCategory",
		"ra.landlord.room.searchWithRental",
		"ra.landlord.room.searchWithExpirationReminder",
		"ra.landlord.agreement.addAgreement",
		"ra.landlord.agreement.updateAgreement",
		"ra.landlord.tenantUser.updateTenantUser",
		"ra.landlord.room.undoThrowLease",
		"ra.landlord.meter.searchMeterByFeeRule",
		"ra.landlord.meter.deleteMeter",
		"ra.landlord.room.searchWithMeter",
		"ra.landlord.meter.addMeters",
		"ra.landlord.fee.deleteFee",
		"ra.landlord.building.searchBuilding",
		"ra.landlord.room.searchWithFee",
		"ra.landlord.room.searchWithFeeReminder",
		"ra.landlord.fee.updateFee",
		"ra.landlord.fee.calculateNextFee",
		"ra.landlord.building.search",
		"ra.landlord.room.searchWithRent",
		"ra.landlord.room.throwLease",              
		"ra.rent.roomInfo.searchRoomInfos",
		"ra.rent.roomInfo.updateRoomInfo",			//RA module end
		"da.finance.daFinanceCategory.search",      //DA module begin
		"da.finance.daFinanceStatement._update",
		"da.finance.daFinanceStatement.search",
		"da.finance.financeStatistics.analyse",
		"da.landlord.building.search",
		"da.landlord.daChargeBill.print",
		"da.landlord.daChargeBill.search",
		"da.landlord.daChargeItem.chargeIndex",
		"da.landlord.daChargeItem.showItems",
		"da.landlord.daMeter.getSuiteMeters",
		"da.landlord.daMeter.searchMeterWithSuite",
		"da.landlord.daMeter.searchSuiteWithMeter",
		"da.landlord.daMeter.showMeterFeeRuleCfg",
		"da.landlord.daMeter.showMeterFeeRuleCfgForSuite",
		"da.landlord.daOwner._update",
		"da.landlord.daOwner.search",
		"da.landlord.daOwner.view",
		"da.landlord.daSuite._rentRoom",
		"da.landlord.daSuite._unRent",
		"da.landlord.daSuite._update",
		"da.landlord.daSuite.addSuite",
		"da.landlord.daSuite.manager",
		"da.landlord.daSuite.rentRoom",
		"da.landlord.daSuite.view",
		"da.landlord.daSuite.viewRoom"           //DA module begin		
	};
	
	@Resource
	private GenericService genericService;
	
	public void initAuthData()
	{
		String modelSimpleName;
		String method;
		String name = "TODO_Name";
		ScopeType scope = ScopeType.Basic;
		for (String srcPriv : privArray)
		{
			int splitIndex = srcPriv.lastIndexOf(".");
			modelSimpleName = srcPriv.substring(0, splitIndex);
			method = srcPriv.substring(splitIndex + 1, srcPriv.length());
			
			Privilege privilege = new Privilege();
			privilege.setModelSimpleName(modelSimpleName);
			privilege.setMethod(method);
			Privilege dbPrivilege = (Privilege)genericService.searchAll(privilege).getData(0);
			if (dbPrivilege != null) 
			{
				 privilege = dbPrivilege;
			}
			privilege.setName(name);
			privilege.setScopeType(scope);
			 
			genericService.merge(privilege);
		}
	}
}
